//
// Created by 原帅 on 2022/11/5.
//

#include "RFinisher.h"
#include "RLog.h"
#include "RFinProcIF.h"
#include "algorithm"

int RFinisher::finish() {
    RLogDbg("start finish process...")
    if (this->finProcessors.empty())
    {
        RLogDbg("finish process vector is empty! nothing to do!")
    }
    else
    {
        for (auto finProc : this->finProcessors) {
            if (finProc == nullptr)
            {
                RLogErr("an finProc is nullptr in finish vector,auto remove it")
                this->finProcessors.erase(std::find(finProcessors.begin(), finProcessors.end(), finProc));
                continue;
            }
            else
            {
                int iRet = finProc->finProc();
                if (0 != iRet) {
                    finProc->finFail(iRet);
                }
            }
        }
    }
    RLogDbg("fin process done!")
    return 0;
}

int RFinisher::registerFinProc(RFinProcIF *rFinProcIf) {
    if (std::find(finProcessors.begin(),finProcessors.end(),rFinProcIf)==finProcessors.end())
    {
        finProcessors.push_back(rFinProcIf);
    }
    else
    {
        RLogErr("there is a same interface already in finish process vector");
        return -1;
    }
    return 0;
}
